package com.xjgc.ws.mes.dao;

import com.xjgc.common.config.DbConn;
import com.xjgc.ws.util.CheckData;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;

/**
 * ----物料出入库接口--------
 * @author jjg-pc
 *实现单表的XML数据解析并组装成SQL语句执行
 */
@Component
public class ZxjMesMblnr {

	@Autowired
	DbConn db;

	@Autowired
	private CheckData checkData;
	/**
	 * 解析报文xml
	 * 根据联合主键删除操作数据del
	 * 执行插入操作sql
	 * 返回执行结果：操作结果记录
	 * @param xml
	 * @return
	 * @throws SQLException
	 */
	public String mblnr(String xml) throws SQLException{
		
		String rs = null;
		PreparedStatement delPst = null;
		PreparedStatement inPst = null;

		//定义执行SQL脚本
		String delSql = "delete from ZXJ_MES_MBLNR where \"MBLNR\"=? and \"ZEILE\"=? and \"MJAHR\"=? and \"WERKS\"=?"; //删除SQL
		String inSql = "insert into ZXJ_MES_MBLNR values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";  //27插入SQL
		//创建数据库连接
		delPst = db.getConnetion().prepareStatement(delSql);
		inPst = db.getConnetion().prepareStatement(inSql);
		int count=0;
		//解析xml文件
		Document doc = null;
		
		try {
			doc = DocumentHelper.parseText(xml);
			Element root = doc.getRootElement();
			Iterator<?> it = root.elementIterator();
			
			while(it.hasNext()) {
				
				Element element = (Element) it.next();

				delPst.setString(1, element.elementText("MBLNR"));
				delPst.setString(2, element.elementText("ZEILE"));
				delPst.setString(3, element.elementText("MJAHR"));
				delPst.setString(4, element.elementText("WERKS"));
				
				inPst.setString(1, element.elementText("MBLNR"));
				inPst.setString(2, element.elementText("ZEILE"));
				inPst.setString(3, element.elementText("MJAHR"));
				inPst.setString(4, element.elementText("WERKS"));
				inPst.setString(5, element.elementText("BLDAT"));
				inPst.setString(6, element.elementText("BUDAT"));
				inPst.setString(7, element.elementText("CPUDT"));
				inPst.setString(8, element.elementText("USNAM"));
				inPst.setString(9, element.elementText("NAME1"));
				inPst.setString(10, element.elementText("LGORT"));
				inPst.setString(11, element.elementText("BWART"));
				inPst.setString(12, element.elementText("SOBKZ"));
				inPst.setString(13, element.elementText("MENGE"));
				inPst.setString(14, element.elementText("DMBTR"));
				inPst.setString(15, element.elementText("MATNR"));
				inPst.setString(16, element.elementText("MAKTX").replace("amp;", "&"));//转义&特殊字符
				inPst.setString(17, element.elementText("BUKRS"));
				inPst.setString(18, element.elementText("LIFNR"));
				inPst.setString(19, element.elementText("EBELN"));
				inPst.setString(20, element.elementText("EBELP"));
				inPst.setString(21, element.elementText("KDAUF"));
				inPst.setString(22, element.elementText("KDPOS"));
				inPst.setString(23, element.elementText("AUFNR"));
				inPst.setString(24, element.elementText("DATUM"));
				inPst.setString(25, element.elementText("UZEIT"));
				inPst.setString(26, "0");
				String uid = checkData.getUidByWekrs(element.elementText("WERKS"));
				inPst.setString(27, uid);

				//删除更新数据
				delPst.executeUpdate();
				
				//插入更新数据
				int row = inPst.executeUpdate();
				if(row>0) {
					count++;
				}
			}
			if (count>0) {
				rs = "0";//执行成功
			}
		} catch (DocumentException e1) {
			rs=e1.toString();//执行失败
			e1.printStackTrace();
		}
		//关闭连接
		delPst.close();
		inPst.close();
		db.closeCn();
		return rs;
	}
}
